<?php
/**
 * 
 * 购物 衣服 处理类
 * @author tanzhe
 *
 */
class ShopModel {
	private $db;
	private $cache;
	private $shopRequire;
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
		global $shopRequire;
		$this->shopRequire = $shopRequire;
	}
	
	public function getShop($key, $shopId) { //获得衣服商店信息
		//print_r($this->shopRequire);
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		//判断是否满足进入条件 
		if ( isset($this->shopRequire[$shopId]) ) {
			if ( $this->shopRequire[$shopId]['hanger'] > $playerInfo['hanger'] ) {
				return array('code'=>206,'ret'=>'');
			}
		} else {
			return array('code'=>205,'ret'=>'');
		}
		$shop = $this->cache->get('shop_'.$shopId);
		if ( empty($shop) ) {
			$query = $this->db->query("select `label` from clothes where `shop_id`='{$shopId}' order by `hotness`,`hotness_type` desc");
			$data = $this->db->fetchAllByQuery($query);
			$shop = array();
			foreach ( $data as $v ) {
				$shop[] = $v['label'];
			}
			$this->cache->set('shop_'.$shopId, $shop);
		}
		return array('code'=>0,'ret'=>array('shop'=>$shop));
	}

	/**
	 * 
	 * 购买衣服
	 * @param str $key
	 * @param str $label 要购买的衣服标识
	 */
	function buy($key, $label) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		
		if ( !empty($label) ) {
			$clothesData = $this->cache->get('clothes_'.$label);
			if ( empty($clothesData) ) {
				$query = $this->db->query("select * from clothes where `label`='{$label}'");
				$clothesData = $this->db->fetchOneByQuery($query);
				$this->cache->set('clothes_'.$label, $clothesData);
			}
			if ( empty($clothesData) ) { //不存在该衣服
				return array('code'=>207,'ret'=>'');
			}
		} else {
			return array('code'=>207,'ret'=>'');
		}
		$totalCoin = 0;
		$totalCash = 0;
		if ( $clothesData['cost_type']==1 ) {
			$totalCoin = $clothesData['price'];
		} else {
			$totalCash = $clothesData['price'];
		}
		//判断购买条件
		if ( isset($this->shopRequire[$clothesData['shop_id']]) ) {
			if ( $this->shopRequire[$clothesData['shop_id']]['hanger'] > $playerInfo['hanger'] ) {
				return array('code'=>206,'ret'=>'');
			}
		} else {
			return array('code'=>205,'ret'=>'');
		}
		//判断游戏币是否足够
		if ( $totalCoin > $playerInfo['coin'] ) {
			return array('code'=>9,'ret'=>'');
		}
		//判断元宝是否足够
		if ( $totalCash > $playerInfo['cash'] ) {
			return array('code'=>10,'ret'=>'');
		}
		//判断是否已经购买过该衣服
		$userinfoModel = new UserinfoModel();
		$playerClothes = $userinfoModel->getAllClothes($key);
		if ( array_key_exists($label, $playerClothes) ) {
			return array('code'=>208,'ret'=>'');
		}
		//更新数据库与缓存
		$updateData = array();
		$updateData['coin'] = $playerInfo['coin'] - $totalCoin;
		$updateData['cash'] = $playerInfo['cash'] - $totalCash;
		$updateData['hanger'] = $playerInfo['hanger'] + 1;
		$playerInfo = array_merge($playerInfo, $updateData);
		//插入衣服信息
		$insertData = array(
			'u_id'=>$playerInfo['u_id'],
			'label'=>$label,
			'hotness_type'=>$clothesData['hotness_type'],
			'hotness'=>$clothesData['hotness'],
			'is_now'=>0,
		);
		$this->db->insertData('player_clothes', $insertData);
		$playerClothes[$label] = $clothesData['hotness'];
		$this->cache->set('my_clothes_'.$playerInfo['u_id'], $playerClothes);
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( $totalCash > 0 ) {
			$aryData = array(
				'label'=>$label,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'shop',
				'num'=> -$totalCash,
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		//返回个人信息
		$ret = array(
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
			'hanger'=>$playerInfo['hanger'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
}
?>